package com.wef.system.service.impl;

import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.Iterator;
import com.wef.system.entity.MenuGroupTitle;
import com.wef.system.entity.MenuItemTitle;
import com.wef.system.entity.domain.MenuGroupTitleDomain;
import com.wef.system.entity.domain.MenuItemTitleDomain;
import com.gfa4j.exception.ValidateRuntimeException;
import org.hibernate.Hibernate;
import com.gfa4j.hibernate.BaseObject;
import com.gfa4j.service.impl.BaseServiceImpl;
import com.gfa4j.util.StringUtils;
import com.wef.system.service.MenuGroupTitleService;

public class MenuGroupTitleServiceImpl extends BaseServiceImpl implements MenuGroupTitleService{

	public MenuGroupTitle addMenuGroupTitle(MenuGroupTitle menuGroupTitle) {
		validateMenuGroupTitle(menuGroupTitle);
		MenuGroupTitle newMenuGroupTitle = (MenuGroupTitle)super.saveObject(menuGroupTitle);
		return newMenuGroupTitle;
	}

	public MenuGroupTitle updateMenuGroupTitle(MenuGroupTitle menuGroupTitle) {
		return (MenuGroupTitle)super.updateObject(menuGroupTitle);
	}

	public void removeMenuGroupTitle(MenuGroupTitle menuGroupTitle) {

		super.deleteObject(menuGroupTitle);
	}

	public void removeMenuGroupTitles(Set<MenuGroupTitle> menuGroupTitles){
		for (MenuGroupTitle menuGroupTitle : menuGroupTitles){
			removeMenuGroupTitle(menuGroupTitle);
		}
	}

	public MenuGroupTitle saveOrUpdateMenuGroupTitle(MenuGroupTitle menuGroupTitle) {
		if (menuGroupTitle.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			return this.addMenuGroupTitle(menuGroupTitle);
		}
		if (menuGroupTitle.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			return this.updateMenuGroupTitle(menuGroupTitle);
		}
		if (menuGroupTitle.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			this.removeMenuGroupTitle(menuGroupTitle);
			return MenuGroupTitle.NULL;
		}
		return menuGroupTitle;
	}

	public void saveOrUpdateMenuGroupTitles(List<MenuGroupTitle> menuGroupTitles,String menuGroupId) {
		StringBuilder errorString = new StringBuilder();
		for (Iterator it = menuGroupTitles.iterator(); it.hasNext(); ) {
			MenuGroupTitle menuGroupTitle = (MenuGroupTitle)it.next();
			if (menuGroupTitle != null)
			{
				int rowStatus = menuGroupTitle.getRowStatus();
				if (menuGroupTitle.getMenuGroupId() == null || menuGroupTitle.getMenuGroupId() == "")
				{
					menuGroupTitle.setMenuGroupId(menuGroupId);
				}
				try {
					menuGroupTitle = this.saveOrUpdateMenuGroupTitle(menuGroupTitle);
				} catch (ValidateRuntimeException ex) {
					errorString.append(ex.getMessage() + "\n");
				}
				if (rowStatus == BaseObject.ROWSTATUS_DELETED)
				{
					it.remove();
				}
			}
		}
		if (errorString.length() > 0)
		{
			throw new ValidateRuntimeException(errorString.toString());
		}
	}

	public void saveOrUpdateMenuGroupTitles(Set<MenuGroupTitle> menuGroupTitles,String menuGroupId) {
		StringBuilder errorString = new StringBuilder();
		for (Iterator it = menuGroupTitles.iterator(); it.hasNext(); ) {
			MenuGroupTitle menuGroupTitle = (MenuGroupTitle)it.next();
			if (menuGroupTitle != null)
			{
				int rowStatus = menuGroupTitle.getRowStatus();
				if (menuGroupTitle.getMenuGroupId() == null || menuGroupTitle.getMenuGroupId() == "")
				{
					menuGroupTitle.setMenuGroupId(menuGroupId);
				}
				try {
				menuGroupTitle = this.saveOrUpdateMenuGroupTitle(menuGroupTitle);
				} catch (ValidateRuntimeException ex) {
					errorString.append(ex.getMessage() + "\n");
				}
				if (rowStatus == BaseObject.ROWSTATUS_DELETED)
				{
					it.remove();
				}
			}
		}
		if (errorString.length() > 0)
		{
			throw new ValidateRuntimeException(errorString.toString());
		}
	}

	public void addMenuGroupTitles(Set<MenuGroupTitle> menuGroupTitles, String menuGroupId) {
		StringBuilder errorString = new StringBuilder();
		try {
			deleteObjectByProperty(MenuGroupTitle.class, MenuGroupTitleDomain.menuGroupId, menuGroupId);
		} catch (ValidateRuntimeException ex) {
			errorString.append(ex.getMessage() + "\n");
		}
		
		for (Iterator<MenuGroupTitle> it = menuGroupTitles.iterator(); it.hasNext();) {
			MenuGroupTitle menuGroupTitle = (MenuGroupTitle) it.next();
			menuGroupTitle.setMenuGroupId(menuGroupId);
			this.addMenuGroupTitle(menuGroupTitle);
		}	
		if (errorString.length() > 0) {
			throw new ValidateRuntimeException(errorString.toString());
		}
	}
	
	public void validateMenuGroupTitle(MenuGroupTitle menuGroupTitle){

	}

}
